package cn.wolfcode.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.wolfcode.domain.Appointment;
import cn.wolfcode.mapper.AppointmentMapper;
import cn.wolfcode.query.AppointmentQueryObject;
import cn.wolfcode.service.IAppointmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AppointmentService implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return appointmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Appointment record) {
        return appointmentMapper.insert(record);
    }

    @Override
    public Appointment selectByPrimaryKey(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Appointment> selectAll() {
        return appointmentMapper.selectAll();
    }

    @Override
    public int updateByPrimaryKey(Appointment record) {
        return appointmentMapper.updateByPrimaryKey(record);
    }

    @Override
    public PageInfo<Appointment> list(AppointmentQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),"id asc");
        List<Appointment> list = appointmentMapper.selectForList(qo);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public void save(Appointment appointment) {
        //设置创建时间
        appointment.setCreate_time(new Date());
        //设置流水号  年月日时分秒+5位随机数组成
        String format = DateUtil.format(new Date(), "yyyyMMddHHmm");
        String numbers = RandomUtil.randomNumbers(5);
        appointment.setAno(format+numbers);
        appointmentMapper.insert(appointment);
    }
}
